package session.stateless;

import entity.Customer;
import entity.LineItem;
import entity.PurchaseOrder;
import entity.SaleLead;
import entity.SaleQuotation;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PurchaseOrderBean implements PurchaseOrderBeanLocal {
    @PersistenceContext()
    EntityManager em;
    PurchaseOrder purchaseOrder;
    @EJB
    CustomerBeanLocal customerSessionBeanLocal;
    @EJB
    SaleLeadBeanLocal saleLeadBeanLocal;
    @EJB
    private DiscountBeanLocal discountBeanLocal;
    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    
    public Long convertToPurchaseOrder(String reg_num, Long saleQuotationId, List<LineItem> lineItems) 
    {
        Customer customer = em.find(Customer.class, reg_num);
        
        if (customer == null)
        {
            SaleLead saleLead = em.find(SaleLead.class, reg_num);
            if (saleLead != null)
            {
                customerSessionBeanLocal.convertCustomerFromSaleLead(saleLead.getRegistrationNumber());
                customer = em.find(Customer.class, reg_num);
            }            
        }
        
        SaleQuotation saleQuotation = em.find(SaleQuotation.class, saleQuotationId);        
        
        List<LineItem> temp_lineItems = new ArrayList<LineItem>();
        for(LineItem o: lineItems) 
        {
            LineItem temp_lineItem = o;
            temp_lineItems.add(temp_lineItem);
        }
        
        purchaseOrder = new PurchaseOrder();
       
        
        purchaseOrder.setCustomer(customer);
        purchaseOrder.setSaleQuotation(saleQuotation);
        purchaseOrder.setLineItems(lineItems);
        Date date = new Date();
        purchaseOrder.setPurchaseOrderDate(dateFormat.format(date));
        em.persist(purchaseOrder);
        
        List<PurchaseOrder> temp_PurchaseOrders;
        try 
        {
            temp_PurchaseOrders = customer.getPurchaseOrders();
        }
        catch (NullPointerException e) 
        {
            temp_PurchaseOrders = new ArrayList<PurchaseOrder>();
        }
            temp_PurchaseOrders.add(purchaseOrder);
            //customer.setPurchaseOrders(temp_PurchaseOrders);

        
        return purchaseOrder.getId();
    }
    
    public Long createDirectPurchaseOrder_SaleLead(String reg_num, List<LineItem> lineItems) 
    {          
        customerSessionBeanLocal.convertCustomerFromSaleLead(reg_num);
        Customer customer = em.find(Customer.class, reg_num);

        purchaseOrder = new PurchaseOrder();
        purchaseOrder.setCustomer(customer);
        purchaseOrder.setLineItems(lineItems);
        Date date = new Date();
        purchaseOrder.setPurchaseOrderDate(dateFormat.format(date));

        em.persist(purchaseOrder);

        List<PurchaseOrder> temp_purchaseOrders;
        try {
            temp_purchaseOrders = customer.getPurchaseOrders();
        } catch (NullPointerException e) {
            temp_purchaseOrders = new ArrayList<PurchaseOrder>();
        }
        temp_purchaseOrders.add(purchaseOrder);
        customer.setPurchaseOrders(temp_purchaseOrders);
        em.flush();

        return purchaseOrder.getId();
    }
    
    public Long createDirectPurchaseOrder_Customer(String reg_num, List<LineItem> lineItems)
    {
        purchaseOrder = new PurchaseOrder();
        
        Customer customer=em.find(Customer.class, reg_num);
        purchaseOrder.setCustomer(customer);

        purchaseOrder.setLineItems(lineItems);
        Date date =new Date();
        purchaseOrder.setPurchaseOrderDate(dateFormat.format(date));
        em.persist(purchaseOrder);
        
        List<PurchaseOrder> temp_purchaseOrders;
        customer.getPurchaseOrders().add(purchaseOrder);
        em.flush();
        
        return purchaseOrder.getId();
    }
    
    
    public List<PurchaseOrder> getPurchaseOrders() {
        Query q = em.createQuery("SELECT c from PurchaseOrder c");
        return q.getResultList();
    }
    
    public List<PurchaseOrder> getPurchaseOrdersByCustomer(String reg_num) {
        Customer customer = em.find(Customer.class, reg_num);

        return customer.getPurchaseOrders();
    }
    
    public void updatePurchaseOrder(Long purchaseOrderId, List<LineItem> lineItems){
        PurchaseOrder purchaseOrder = em.find(PurchaseOrder.class, purchaseOrderId);
        if (purchaseOrder == null)
            System.out.println("Sale Order does not exist. Please try again.");
        else
        {
            purchaseOrder.setLineItems(lineItems);
            em.flush();
        }
    }
    
    public void deletePurchaseOrder(Long purchaseOrderId)
    {
        PurchaseOrder purchaseOrder = em.find(PurchaseOrder.class, purchaseOrderId);
        if(purchaseOrder == null)
            System.out.println("No Sale Order Found.");
        else
           {
//            Customer temp_customer = purchaseOrder.getCustomer();
//            List<PurchaseOrder> temp_purchaseOrders = temp_customer.getPurchaseOrders();
//            temp_purchaseOrders.remove(purchaseOrder);
//            temp_customer.setPurchaseOrders(temp_purchaseOrders);
//          
            List<LineItem> temp=purchaseOrder.getLineItems();
            purchaseOrder.setLineItems(null);
            
            for (LineItem lineItem: temp)
            {
                em.remove(lineItem);
            }
            purchaseOrder.getCustomer().getPurchaseOrders().remove(purchaseOrder);
            em.flush();
            em.remove(purchaseOrder);
            em.flush();
        }
    }

}
